﻿'Params
'limit (Optional) : The number of results to fetch per page. Defaults to 50.
'page (Optional) : The page number to fetch. Defaults to first page.
'artist (Required) : The artist name
'api_key (Required) : A Last.fm API key.

'Auth
' This service does not require authentication. 

Public Structure ArtistSearchResponse
	Dim lfmstatus As String
	Dim artists() As Artist
End Structure

Public Class ArtistSearch
	Inherits BaseRequest
	Dim artist As String = ""
	Dim limit As String = ""
	Dim page As String = ""

	Public xmlresponse As XDocument = Nothing

	Public Sub New(artist As String, limit As String, page As String)
		Me.artist = artist
		Me.limit = limit
		Me.page = page
	End Sub

	Public Sub sendRequest()
		InitializeMembers("Artist.Search")

		AddMember("limit", limit, False)
		AddMember("page", page, False)
		AddMember("artist", artist, True)
		AddMember("api_key", My.Settings.api_key, False)

		xmlresponse = sendPOSTResponse()
	End Sub

	Public Function getResponse() As ArtistSearchResponse
		Dim response As ArtistSearchResponse = Nothing
		Dim count As Integer

		Try
			response.lfmstatus = xmlresponse.<lfm>.Attributes.ElementAt(0).Value
		Catch ex As Exception
			If response.lfmstatus <> "ok" Then
				Return Nothing
			End If
		End Try

		Try
			count = xmlresponse.<lfm>.<results>.<artistmatches>.<artist>.Count()
		Catch ex As Exception
			Return response
		End Try
		If (count > 0) Then
			ReDim response.artists(count - 1)
			For i As Integer = 0 To count - 1
				response.artists(i) = New Artist(xmlresponse.<lfm>.<results>.<artistmatches>.<artist>(i))
			Next
		End If
		Return response
	End Function

End Class
